package com.ship.controller;

import com.ship.commens.Result;
import com.ship.dto.UserDto;
import com.ship.dto.UserSearchDto;
import com.ship.service.UserService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/getUsersByPage.do")
    public Result getUsersByPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        return userService.findUsersByPage(pageNum, pageSize);
    }

    @RequestMapping("/addUsers.do")
    public Result addUsers(@Validated UserDto userDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }

        return userService.saveUser(userDto);
    }

    //单个删除
    @PostMapping("/cutOneUser.do")
    public Result cutOneUser(Long id){
        if(id < 1){
            return Result.DATA_FORMAT_ERROR;
        }

        return userService.removeOneUser(id);
    }

    //批量删除
    @RequestMapping("/cutManyUser.do")
    public Result cutManyUser(String id){
        return userService.removeManyUser(id);
    }

    //修改员工信息
    @RequestMapping("/editUser.do")
    public Result editUser(@Validated UserDto userDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }
        return userService.modifyUser(userDto);
    }

    //多条件查询
    @RequestMapping("/getUsersBySearch.do")
    public Result getUsersBySearch(@Validated UserSearchDto userSearchDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }

        return userService.findUsersBySearch(userSearchDto);
    }
}
